/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.karelun.ideaboard.gef.ui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.ui.IWorkbenchPart;
import org.karelun.ideaboard.gef.Disposable;

/**
 * Base class for actions involving a WorkbenchPart. The workbench part is
 * useful for obtaining data needed by the action. For example, selection can be
 * obtained using the part's site. Anything can potentially be obtained using
 * {@link org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)}.
 */
public abstract class WorkbenchPartAction extends Action implements Disposable,
		UpdateAction {

	private IWorkbenchPart workbenchPart;
	
	private boolean lazyEnablement = true;

	public WorkbenchPartAction(IWorkbenchPart part) {
		setWorkbenchPart(part);
		init();
	}

	public WorkbenchPartAction(IWorkbenchPart part, int style) {
		super(null, style);
		setWorkbenchPart(part);
		init();
	}

	protected abstract boolean calculateEnabled();

	public void dispose() {
	}

//	protected void execute(Command command) {
//		if (command == null || !command.canExecute())
//			return;
//		getCommandStack().execute(command);
//	}

	protected IWorkbenchPart getWorkbenchPart() {
		return workbenchPart;
	}

	protected void init() {
	}

	public boolean isEnabled() {
		if (lazyEnablement)
			setEnabled(calculateEnabled());
		return super.isEnabled();
	}

	protected void refresh() {
		setEnabled(calculateEnabled());
	}

	
	public void setLazyEnablementCalculation(boolean value) {
		lazyEnablement = value;
	}

	protected void setWorkbenchPart(IWorkbenchPart part) {
		workbenchPart = part;
	}

	public void update() {
		refresh();
	}
	
//	
}
